﻿<?PHP
require_once('config.inc.php');
require_once('database.inc.php');
require_once('functions.inc.php');
require_once(LIBRARY_PATH.'json/JSON.php');
require('security.php');

$db = LinkDB();
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);

//初始化参数
$getAll = @$_GET['getall'];
$keyWord = @$_GET['keyword'];
$noncePage = @$_GET['page'];
$orderBy = @$_GET['orderby'];
$noncePage = $noncePage ? $noncePage : 1;
$getAll = $getAll=='true' ? true : false;
$itemLimit = 20;

//获取符合要求的记录数量
$whereString = $keyWord ? ' WHERE name like '.FP($keyWord,'%','%') : '';
$sqlString = 'SELECT count(*) FROM people'.$whereString;
$count = $db->GetOne($sqlString);
if (!$count && $db->ErrorMsg()) feedback(false, '[数据库错误]'.$db->ErrorMsg());
if ($count == 0) feedback(false, $keyWord ? '[未找到符合要求的联系人]' : '[暂无数据]');

//计算分页信息
$pageCount = ceil($count/$itemLimit);
$noncePage = ($noncePage > $pageCount ? $pageCount : $noncePage);
$stratPoint = $itemLimit * ( $noncePage - 1 );

//获取指定页的记录数组
$limitString = $getAll ? '' : ' LIMIT '.$stratPoint.' , '.$itemLimit;
$orderString = ' ORDER BY '.($orderBy ? $orderBy : 'created DESC');
$sqlString = 'SELECT id,name,brief FROM people'.$whereString.$orderString.$limitString;
$list = $db->GetAll($sqlString);
if (!$list && $db->ErrorMsg()) feedback(false, '[数据库错误]'.$db->ErrorMsg());

//返回值
$result = array(
    'isSucceed'=> true,
    'page'     => array(
        'noncePage'      => $noncePage,
        'itemLimitOfPage'=> $itemLimit,
        'itemCount'      => $count
    ),
    'list'     => $list
);
echo @$json->encode($result);
/**
 * 数据模板
{
'isSucceed': true,
'page':{
	'noncePage':4,
	'itemLimitOfPage':10,
	'itemCount':155
},
'list':[
	{'id'	: 1,
	 'name' : '测试姓名1',
	 'brief': '测试简介1'},
	{'id'	: 2,
	 'name' : '测试姓名2',
	 'brief': '测试简介2'},
	{'id'	: 3,
	 'name' : '测试姓名3',
	 'brief': '测试简介3'},
	{'id'	: 4,
	 'name' : '测试姓名4',
	 'brief': '测试简介4'}
]
}
*/
?>